package com.pcj.blog.service;/**
 * @param
 * @author pcj
 * @date 2021/12/29 11:28
 * @return
 */

import com.pcj.blog.NotFoundException;
import com.pcj.blog.dao.BlogRepository;
import com.pcj.blog.po.Blog;
import com.pcj.blog.po.Tag;
import com.pcj.blog.po.Type;
import com.pcj.blog.util.MarkDownUtil;
import com.pcj.blog.util.MyBeanUtil;
import com.pcj.blog.vo.BlogVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import javax.transaction.Transactional;
import javax.xml.crypto.Data;
import java.util.*;

/**
 *
 * @className BlogServiceImpl
 * @author pengcj
 * @date 2021/12/29 11:28
 */

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogRepository blogRepository;
    @Override
    public Blog getBlog(Long id) {
        return blogRepository.getById(id);
    }

    @Transactional
    @Override
    public Blog getAndConverd(Long id) {
        Blog b=blogRepository.getById(id);
        if(b==null){
            throw new  NotFoundException("博客不存在");
        }
        Blog blog=new Blog();
        BeanUtils.copyProperties(b,blog);
        String content=blog.getContent();
        blog.setContent( MarkDownUtil.markdownToHtmlExtensions(content));
        blogRepository.updateViews(id);
        return blog;
    }


    @Override
    public Page<Blog> listBlog(Pageable pageable, BlogVo blog) {
        return blogRepository.findAll(new Specification<Blog>() {
            @Override
            public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate>predicates=new ArrayList<>();
                if(!"".equals(blog.getTitle())&&(blog.getTitle()!=null)){
                    predicates.add(cb.like(root.<String>get("title"),"%"+blog.getTitle()+"%"));
                }
                if(blog.getId()!=null){
                    predicates.add(cb.equal(root.<Type>get("type").get("id"),blog.getId()));
                }
                if (blog.isRecommende()){
                    predicates.add(cb.equal(root.<Boolean>get("recommende"),blog.isRecommende()));
                }
                // 相当于 sql 里面的where   传一个数组
                query.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        },pageable);
    }

    @Transactional
    @Override
    public Blog saveBlog(Blog blog) {
        if(blog.getId()<1){  //说明id不存在  是新增博客
            blog.setCreatTime(new Date());
            blog.setUpdateTime(new Date());
            blog.setViews(0);
        }else {
            blog.setUpdateTime(new Date());
        }
        return blogRepository.save(blog);
    }

    @Transactional
    @Override
    public Blog updateBlog(Long id, Blog blog) {
        Blog b=blogRepository.getById(id);
        if(b==null){
            throw new NotFoundException("该博客不存在");
        }
        BeanUtils.copyProperties(blog,b, MyBeanUtil.getNullPropertyNames(blog));  //忽略属性值为空的字段
        b.setUpdateTime(new Date());
        return blogRepository.save(b);
    }

    @Transactional
    @Override
    public void deleteBlog(Long id) {
        blogRepository.deleteById(id);
    }

    @Override
    public Page<Blog> listBlog(Pageable pageable) {
        return blogRepository.findAll(pageable);
    }

    @Override
    public Page<Blog> listBlog(String query, Pageable pageable) {
        return blogRepository.findByQuery(query,pageable);
    }

    @Override
    public List<Blog> listTop(Integer size) {
        Sort sort=Sort.by(Sort.Direction.DESC,"updateTime");
        Pageable pageable=PageRequest.of(0,size,sort);
        return blogRepository.findTop(pageable);
    }

    @Override
    public Page<Blog> ListBlog(Long tagid, Pageable pageable) {
        return blogRepository.findAll(new Specification<Blog>() {
            @Override
            public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //关联查询
                Join join=root.join("tags");
                return cb.equal(join.get("id"),tagid);//会查询出和tagid相等的博客
            }
        },pageable);
    }

    //博客归档
    @Override
    public Map<String, List<Blog>> archiveBlog() {
        //1. 拿到所有的年份
        List<String> years=blogRepository.findGroupYears();
        Map<String,List<Blog>> map=new HashMap<>();
        for(String year : years){
            map.put(year,blogRepository.findByYear(year));
        }
        return map;
    }

    @Override
    public Long countBlog() {
        return blogRepository.count();
    }
}
